package com.zhehekeji.opsys.api.service.validate;

import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zhehekeji.opsys.api.code.LuckySigninResultCode;
import com.zhehekeji.opsys.api.common.util.SuperResp;
import com.zhehekeji.opsys.api.entity.TSignin;
import com.zhehekeji.opsys.api.service.validate.args.SigninValidateArgs;

/**
 * @ClassName SigninValidate
 * @Description TODO
 * @author chenweiliu chenwei.liu@163.com
 * @date 2017-4-5   上午10:03:28
 */
@Service
public class SigninValidate extends ValidateService<ValidateRets<TSignin>, SigninValidateArgs>{

	private final static Logger logger = LoggerFactory.getLogger(SigninValidate.class);
	
	@Override
	public ValidateRets<TSignin> validate(SigninValidateArgs validateArgs)
			throws Exception {
		
		ValidateRets<TSignin> validateRets = new ValidateRets<TSignin>();
		SuperResp resp = validateArgs.getResp();
		TSignin tmpTSignin = validateArgs.getTsignin();
		Date currentDate = validateArgs.getCurrentDate();
		Integer tmpStatus = tmpTSignin.getStatus();
		if (tmpStatus == 0){
			logger.error("签到活动已经停用！");
			resp.setResultCode(LuckySigninResultCode.SIGNIN_HAS_BEEN_STOPPED);
			resp.setResultDesc("专题活动已经停用！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		Date tmpStartTime = tmpTSignin.getStartTime();
		Date tmpEndTime = tmpTSignin.getEndTime();
		if (tmpStartTime == null || tmpEndTime == null){
			logger.error("数据异常！");
			resp.setResultCode(LuckySigninResultCode.DATABASE_EXCEPTION);
			resp.setResultDesc("数据异常！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (currentDate.compareTo(tmpStartTime) <0){
			logger.error("签到活动还未开始！");
			resp.setResultCode(LuckySigninResultCode.SIGNIN_HAS_YET_TO_BEGIN);
			resp.setResultDesc("签到活动还未开始！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		if (currentDate.compareTo(tmpEndTime) >0){
			logger.error("专题活动已经结束！");
			resp.setResultCode(LuckySigninResultCode.SIGNIN_HAS_COME_TO_AN_END);
			resp.setResultDesc("专题活动已经结束！");
			validateRets.setSuccess(false);
			return validateRets;
		}
		validateRets.setEntity(tmpTSignin);
		return validateRets;
	}

}

